package com.freshcity.page.wait
{
	import com.freshcity.component.BaseGameObject;
	import com.freshcity.component.WaitIcon;
	import com.freshcity.component.guide.GuideArrowManager;
	import com.freshcity.component.guide.GuideTipsManager;
	import com.freshcity.component.guide.LineBoxTips;
	import com.freshcity.core.OpCodes;
	import com.freshcity.core.SoundTypeConst;
	import com.freshcity.graphics.RoomInfoWin;
	import com.freshcity.graphics.WaitTalkWin;
	import com.freshcity.manager.EffectManager;
	import com.freshcity.manager.LayerManager;
	import com.freshcity.page.search.SelectTalker;
	import com.freshcity.page.talk.Talk;
	import com.freshcity.utils.StringUtil;
	import com.greensock.TweenLite;
	import com.greensock.TweenMax;
	
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.external.ExternalInterface;
	import flash.geom.Point;
	import flash.text.TextField;

	public class RoomList extends BaseGameObject
	{
		public var win:RoomInfoWin;
		public var queryRoom:*;

		public var lineBox:LineList;
		public var waitTalk:WaitTalk;

		private var mask:Shape;
		private var cityList:CityList;
		private var returnWorldLink:TextField;

		//public var guide:RoomListGuide;

		override public function initialize():void
		{
			super.initialize();

			win=new RoomInfoWin();
			win.visible=false;

			LayerManager.sceneLayer.centerPopup(win);
			win.x+=800;
			parent.addChild(win);

			mask=new Shape();
			mask.graphics.beginFill(0xffffff);
			mask.graphics.drawRect(10, 75, 380, 300);

			lineBox=new LineList(null, 390, 75);
			lineBox.owner=this;
			lineBox.mask=mask;

			waitTalk=new WaitTalk(10, 75);
			waitTalk.owner=this;
			waitTalk.visible=false;

			win.addChild(mask);
			//win.addChild(lineBox);
			win.addChild(waitTalk);

			cityList=new CityList(null, 10, 75);
			cityList.owner=this;
			cityList.mask=mask;
			//win.addChild(cityList);

			returnWorldLink=StringUtil.addHyLink("返回城际中心", "", onVisitFirst);
			returnWorldLink.visible=false;
			returnWorldLink.x=300;
			returnWorldLink.y=45;
			win.addChild(returnWorldLink);

			//guide=new RoomListGuide(this);

			//win.refreshBtn.addEventListener(MouseEvent.CLICK, onRefreshData);
			
		}

		public function startGuide():void
		{
			//guide.onGuideStep1();
		}


		public function onRefreshData(event:MouseEvent=null):void
		{
			group.commu.send(OpCodes.ROOM_LIST, {sid: this.group.user.stop}, show);
		}


		private function onCancel(data:*):void
		{
			group.commu.unRegister(OpCodes.CANCEL_WAIT);
			queryRoom=null;
			waitTalk.hide();
			//this.win.refreshBtn.enabled=true;
			win.tipMsg.text="选择不同主题场所，寻找新朋友。";
			TweenMax.to(this.waitTalk, 0.3, {autoAlpha: 0, onComplete: onRefreshData});
		}

		public function onWaitTalk(data:*):void
		{
			group.commu.unRegister(OpCodes.WAIT_TALK);
			if (data is String)
			{
				group.msg.showRollTipsMsg(data);
			}
			else
			{
				queryRoom=data.room;
				win.tipMsg.text="";
				//this.win.refreshBtn.enabled=false;
				waitTalk.onShow(data.room);
				TweenMax.to(this.waitTalk, 0.3, {onComplete: waitShow, autoAlpha: 1});
			}
		}

		public function show(data:*):void
		{
			win.titleName.text=group.user.stopName;
			win.tipMsg.text="选择不同主题场所，寻找新朋友。";
			returnWorldLink.visible=(String(group.user.stop) != '0');

			var s:RoomInfo;
			var xpos:int=10;
			var ypos:int;

			var roomBox:Sprite=new Sprite();

			data.sortOn("id", Array.NUMERIC);

			for each (var room:* in data)
			{
				if (group.user.level >= room.grade)
				{
					s=new RoomInfo(room, xpos, ypos);
					s.owner=this;
					roomBox.addChild(s);
					ypos+=80;
				}
			}
			win.panel.source=roomBox;
		}

		public function onVisitCities(event:TextEvent):void
		{
			var fun1:Function=function():void
			{
				if (!win.contains(cityList))
				{
					win.addChild(cityList);
				}
			};

			TweenLite.to(this.cityList, 0.5, {onInit: fun1, x: 10});
		}

		public function onVisitFirst(event:TextEvent):void
		{
			group.commu.send(OpCodes.VISIT_CITY, {sid: "0"}, show);
		}

		private function waitShow():void
		{
			if (group.user.guideStep <= 3)
			{
				//guide.onGuideStep5();
				//guide.setCallBack(function():void{});
			}
		}

		public function hide():void
		{
			if (win.contains(cityList))
			{
				win.removeChild(cityList);
			}
		}
	}
}
